<?php

/*
 * Copyright 2012 Clement Sannicolo <clement.sannicolo@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace CSan\NutrientBundle\Controller;

use CSan\UtilBundle\Controller\AbstractController as Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

use Symfony\Component\HttpFoundation\ParameterBag;

use CSan\NutrientBundle\Entity\Food;

/**
 * @Route("/nutrient/food")
 */
class FoodController extends Controller
{
	/**
	 * @Route("/search.{_format}",
	 *      name = "csan_nutrient_food_search",
	 * 	    requirements = {
	 * 	        "_method" = "GET",
	 *          "_format" = "json"
	 * 	    }
	 * )
	 */
	public function searchAction($_format)
	{
		$parameterBag = $this->getParameterBag('GET');
		$parameters = $this->filterParameters($parameterBag);
		$repository = $this->getDoctrine()->getEntityManager()->getRepository('CSanNutrientBundle:Food');
		$foods = $repository->findAll($parameters);

		// Templating
		return $this->render('CSanNutrientBundle:Food:search.'.$_format.'.twig', array(
			'foods' => $foods,
			'count' => count($foods),
		));
	}

	/**
	 * @Route(".{_format}",
	 * 	    name = "csan_nutrient_food_read",
	 *      defaults = {"publicId"=null},
	 * 	    requirements = {
	 *          "_method" = "GET",
	 *          "_format" = "json"
	 * 	    }
	 * ),
	 * @Route("/{publicId}.{_format}",
	 * 	    name = "csan_nutrient_food_read_public_id",
	 * 	    requirements = {
	 *          "_method" = "GET",
	 *          "publicId" = "[a-zA-Z0-9]{6,32}",
	 *          "_format" = "json"
	 * 	    }
	 * )
	 */
	public function readAction($publicId, $_format)
	{	
		// Preprocess
		$parameters = array();
		if (empty($publicId)) {
			$parameterBag = $this->getParameterBag('GET');
			$parameters = $this->filterParameters($parameterBag);
		}
		else {
			$parameters = array('publicId' => $publicId);
		}

		// Process
		$repository = $this->getDoctrine()->getEntityManager()->getRepository('CSanNutrientBundle:Food');
		$foods = $repository->findAll($parameters);
		$count = count($foods);

		// Templating
		return $this->render('CSanNutrientBundle:Food:success.'.$_format.'.twig', array(
			'foods' => $foods,
			'count' => $count,
		));
	}

	/**
	 * @Route(".{_format}",
	 * 		name = "csan_nutrient_food_create",
	 * 		requirements = {
	 * 			"_method" = "POST",
	 * 			"_format" = "json"
	 * 		}
	 *	)
	 */
	public function createAction($_format)
	{	
		// Preprocess
		$publicId = $this->get('csan.util.number')->uuid();
		$food = new Food($publicId);
		$parameterBag = $this->getParameterBag('POST', $_format);
		$this->bind($parameterBag, $food);

		// Validation
		$this->validate($food);

		// Process
		$entityManager = $this->getDoctrine()->getEntityManager();
		$entityManager->persist($food);
		$entityManager->flush();

		// Templating
		return $this->render('CSanNutrientBundle:Food:success.'.$_format.'.twig', array(
			'foods' => array($food),
			'count' => 1,
		));
	}

	/**
	 * @Route("/{publicId}.{_format}",
	 * 	    name = "csan_nutrient_food_update",
	 * 	    requirements = {
	 *          "_method" = "PUT",
	 *          "publicId" = "[a-zA-Z0-9]{6,32}",
	 *          "_format" = "json"
	 * 	    }
	 * )
	 */
	public function updateAction($publicId, $_format)
	{	
		// Preprocess
		$repository = $this->getDoctrine()->getEntityManager()->getRepository('CSanNutrientBundle:Food');
		$food = $repository->findOne(array('publicId' => $publicId));
		$parameterBag = $this->getParameterBag('PUT', $_format);
		$this->bind($parameterBag, $food);

		// Validation
		$this->validate($food);

		// Process
		$entityManager = $this->getDoctrine()->getEntityManager();
		$entityManager->persist($food);
		$entityManager->flush();

		// Templating
		return $this->render('CSanNutrientBundle:Food:success.'.$_format.'.twig', array(
			'foods' => array($food),
			'count' => 1,
		));
	}

	/**
	 * @Route("/{publicId}.{_format}",
	 * 		name = "csan_nutrient_food_destroy",
	 * 		requirements = {
	 * 			"_method"="DELETE",
	 * 			"publicId" = "[a-zA-Z0-9]{6,32}",
	 * 			"_format" = "json"
	 * 		}
	 * 	)
	 */
	public function destroyAction($publicId, $_format)
	{	
		// Preprocess
		$repository = $this->getDoctrine()->getEntityManager()->getRepository('CSanNutrientBundle:Food');
		$food = $repository->findOne(array('publicId' => $publicId));

		// Process
		$entityManager = $this->getDoctrine()->getEntityManager();
		$entityManager->remove($food);
		$entityManager->flush();

		// Templating
		return $this->render('CSanNutrientBundle:Food:success.'.$_format.'.twig', array(
			'foods' => array(),
			'count' => 0,
		));
	}

	/**
	 * Filter parameters for db query
	 * 
	 * @param	ParameterBag 	$parameterBag
	 * @return	array			$parameters
	 */
	public function filterParameters(ParameterBag $parameterBag)
	{
		$parameters = array(
			'name' => $parameterBag->get('name'),
			'limit' => 100,
		);

		return $parameters;
	}

	/**
	 * Bind parameters on food
	 *
	 * @param	Food			$food
	 * @param	ParameterBag 	$parameterBag
	 */
	public function bind(ParameterBag $parameterBag, Food $food)
	{
		$food->setName($parameterBag->get('name'));
		$food->setEnergy($parameterBag->get('energy'));
		$food->setProtein($parameterBag->get('protein'));
		$food->setCarbohydrates($parameterBag->get('carbohydrates'));
		$food->setStarch($parameterBag->get('starch'));
		$food->setSugar($parameterBag->get('sugar'));
		$food->setFat($parameterBag->get('fat'));
		$food->setFattyAcidsSaturated($parameterBag->get('fattyAcidsSaturated'));
		$food->setFattyAcidsMonounsaturated($parameterBag->get('fattyAcidsMonounsaturated'));
		$food->setFattyAcidsPolyunsaturated($parameterBag->get('fattyAcidsPolyunsaturated'));
		$food->setWater($parameterBag->get('water'));
		$food->setAlcohol($parameterBag->get('alcohol'));
		$food->setFiber($parameterBag->get('fiber'));
		$food->setCalcium($parameterBag->get('calcium'));
		$food->setCopper($parameterBag->get('copper'));
		$food->setIodine($parameterBag->get('iodine'));
		$food->setIron($parameterBag->get('iron'));
		$food->setMagnesium($parameterBag->get('magnesium'));
		$food->setManganese($parameterBag->get('manganese'));
		$food->setPhosphorus($parameterBag->get('phosphorus'));
		$food->setPotassium($parameterBag->get('potassium'));
		$food->setSelenium($parameterBag->get('selenium'));
		$food->setSodium($parameterBag->get('sodium'));
		$food->setZinc($parameterBag->get('zinc'));
		$food->setVitaminA($parameterBag->get('vitaminA'));
		$food->setVitaminB1($parameterBag->get('vitaminB1'));
		$food->setVitaminB2($parameterBag->get('vitaminB2'));
		$food->setVitaminB3($parameterBag->get('vitaminB3'));
		$food->setVitaminB5($parameterBag->get('vitaminB5'));
		$food->setVitaminB6($parameterBag->get('vitaminB6'));
		$food->setVitaminB9($parameterBag->get('vitaminB9'));
		$food->setVitaminB12($parameterBag->get('vitaminB12'));
		$food->setVitaminC($parameterBag->get('vitaminC'));
		$food->setVitaminD($parameterBag->get('vitaminD'));
		$food->setVitaminE($parameterBag->get('vitaminE'));
	}
}